package com.qidian.buy.cms.controller;

import com.qidian.buy.cms.util.QiniuTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 * @date 2024/7/18 10:09
 */
@RestController
@CrossOrigin
@RequestMapping("/wangUpload")
public class WangEditorUploadController {
    @Autowired
    private QiniuTools qiniuTools;
    @PostMapping
    public Map wangEditorUpload(@RequestParam("uploadFile") MultipartFile uploadFile){
        Map result = new HashMap();
        // 获取已经上传到服务器的文件的字节数组数据
        byte[] bytes = new byte[0];
        try {
            bytes = uploadFile.getBytes();
            // 使用工具将这个文件上传到云服务器
            String url = qiniuTools.uploadFile(bytes);
            // 上传成功
            result.put("errno",0);
            Map data = new HashMap();
            data.put("url",url);
            data.put("alt","xxx");
            data.put("href","xx");
            result.put("data",data);
        } catch (IOException e) {
            // 记录异常
            result.put("errno",1);
            result.put("message","上传图片失败:" +e.getMessage());
        }
        return result;
    }
}
